# -*- coding: utf-8 -*-
import os
from logger import loggerDefine
import platform


class LocalCmd:
    # 定义通用日志
    logcmd = loggerDefine("LocalCmd", "LocalCmd.log")

    def readLocallist(self, filenamepath=None):
        """
         filename:文件名
        """
        if filenamepath:
            filename = filenamepath
        else:
            root_dir = os.path.abspath('.')  # 获取当前路径
            filename = os.path.join(root_dir, "config/local_hosts.list")

        with open(filename, encoding="utf8") as f:
            for line in f:
                # 不以注释开头且不是空行
                if not line.startswith('#') and not line.isspace():
                    """ 
                    去掉换行符并分割字符串进行分割
                     格式: 命令集合(cmd1,cmd2,cmd3...)
                         localcmds...
                    """
                    cmdlist = line.strip('\n').split(',')
                    #  命令集合(cmd1,cmd2,cmd3...)
                    #  cmd...
                    for cmd in cmdlist:
                        test = os.system(cmd)
                        if test == 0:
                            self.logcmd.info(
                                "本地主机 " + platform.system() + ":" + platform.platform() + "  执行命令-->: " + cmd + "成功!!!")

                        else:
                            self.logcmd.error(
                                "本地主机 " + platform.system() + ":" + platform.platform() + "  执行命令-->: " + cmd + "不成功成功!!!")
                else:
                    self.logcmd.error("配置文件-->: " + filename + " 本地主机" + str(platform.uname()) + "命令列表为空，不能执行远程命令")

    # Press the green button in the gutter to run the script.


if __name__ == '__main__':
    asu = LocalCmd()
    asu.readLocallist()
